package com.gnerv.sylvanas.business.manage.logic;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.framework.core.BaseCurrentAccount;
import com.gnerv.sylvanas.framework.core.BaseUkIdGenerate;
import com.gnerv.sylvanas.business.manage.model.bo.ViewBO;
import com.gnerv.sylvanas.business.manage.model.dto.ViewCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ViewDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ViewRoleDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ViewTreeDTO;
import com.gnerv.sylvanas.business.manage.model.dto.ViewUpdateDTO;
import com.gnerv.sylvanas.business.manage.operation.ViewOperationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class ViewLogicService  {

    private final ViewOperationService viewOperationService;
    private final BaseCurrentAccount baseCurrentAccount;
    private final BaseUkIdGenerate baseUkIdGenerate;

    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(ViewCreateDTO viewCreateDTO) {
        ViewBO viewBO = viewCreateDTO.toViewBO();
        viewBO.setUkId(baseUkIdGenerate.generate());
        viewOperationService.create(viewBO);
        if (viewCreateDTO.getRouteUkId() != null && "".equals(viewCreateDTO.getRouteUkId())) {
            viewOperationService.bindRoute(viewBO.getUkId(), viewCreateDTO.getRouteUkId());
        }
        return RestResult.ok();
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        viewOperationService.deleteByUkId(ukId);
        viewOperationService.unbindRole(ukId);
        return RestResult.ok();
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(ViewUpdateDTO viewUpdateDTO) {
        ViewBO viewBO = viewUpdateDTO.toViewBO();
        viewOperationService.updateByUkId(viewBO);
        viewOperationService.unbindRoute(viewBO.getUkId());
        if (viewUpdateDTO.getRouteUkId() != null && "".equals(viewUpdateDTO.getRouteUkId())) {
            viewOperationService.bindRoute(viewBO.getUkId(), viewUpdateDTO.getRouteUkId());
        }
        return RestResult.ok();
    }

    
    public RestResult<ViewDTO> selectByUkId(String ukId) {
        return RestResult.ok(viewOperationService.selectByUkId(ukId));
    }

    
    public RestResult<List<ViewDTO>> selectListByName(String name) {
        return RestResult.ok(viewOperationService.selectListByName(name));
    }

    
    public RestResult<List<ViewDTO>> selectListByParentUkId(String parentUkId) {
        List<ViewDTO> viewList = viewOperationService.selectListByParentUkId(parentUkId);
        return RestResult.ok(viewList);
    }

    
    public RestResult<List<ViewTreeDTO>> selectListTreeByParentUkId(String parentUkId) {
        List<ViewTreeDTO> viewTreeDTOList = viewOperationService.selectListTreeByParentUkId(parentUkId);
        return RestResult.ok(viewTreeDTOList);
    }

    
    public RestResult<List<ViewTreeDTO>> selectListTreeByCurrentAccount() {
        String ukId = baseCurrentAccount.ukId();
        List<ViewTreeDTO> viewTreeDTOList =  viewOperationService.selectListTreeByAccountUkId(ukId);
        return RestResult.ok(viewTreeDTOList);
    }

    
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> bindRole(ViewRoleDTO viewRoleDTO) {
        viewOperationService.unbindRole(viewRoleDTO.getViewUkId());
        viewOperationService.bindRole(viewRoleDTO.getViewUkId(), viewRoleDTO.getRoleUkIdList());
        return RestResult.ok();
    }

}
